package com.youi.manage;

import com.youi.exception.BusinessException;
import com.youi.util.CommonInfoUtil;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * http request 管理工具
 *
 * @author erdong.Chan
 */
public class HttpRequestManage {

    /**
     * 获取Integer类型数据
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Integer getInteger(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null) {
            throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
        }
        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Integer类型数据	默认返回null
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Integer optInteger(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return null;
        }
        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Integer类型数据
     *
     * @param key
     * @param request
     * @param defaultValue 默认值
     * @return
     * @throws BusinessException
     */
    public Integer optInteger(String key, HttpServletRequest request, Integer defaultValue) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return defaultValue;
        }
        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 获取Long类型数据
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Long getLong(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null) {
            throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
        }
        try {
            return Long.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Long类型数据	默认返回null
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Long optLong(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return null;
        }
        try {
            return Long.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Long类型数据
     *
     * @param key
     * @param request
     * @param defaultValue 默认值
     * @return
     * @throws BusinessException
     */
    public Long optLong(String key, HttpServletRequest request, Long defaultValue) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return defaultValue;
        }
        try {
            return Long.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 获取Float类型数据
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Float getFloat(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null) {
            throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
        }
        try {
            return Float.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Float类型数据	  默认返回null
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Float optFloat(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return null;
        }
        try {
            return Float.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Float类型数据
     *
     * @param key
     * @param request
     * @param defaultValue 默认值
     * @return
     * @throws BusinessException
     */
    public Float optFloat(String key, HttpServletRequest request, Float defaultValue) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return defaultValue;
        }
        try {
            return Float.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 获取Double类型数据
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Double getDouble(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null) {
            throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
        }
        try {
            return Double.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Double类型数据	  默认返回null
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public Double optDouble(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return null;
        }
        try {
            return Double.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Double类型数据
     *
     * @param key
     * @param request
     * @param defaultValue 默认值
     * @return
     * @throws BusinessException
     */
    public Double optDouble(String key, HttpServletRequest request, Double defaultValue) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return defaultValue;
        }
        try {
            return Double.valueOf(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 获取String类型数据
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public String getString(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        System.out.println("==============key:" + key);
        System.out.println("==============value:" + value);
        if (value == null) {
            throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
        }
        try {
            return value;
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取String类型数据	  默认返回null
     *
     * @param key
     * @param request
     * @return
     * @throws BusinessException
     */
    public String optString(String key, HttpServletRequest request) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return null;
        }
        try {
            return value;
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取String类型数据
     *
     * @param key
     * @param request
     * @param defaultValue 默认值
     * @return
     * @throws BusinessException
     */
    public String optString(String key, HttpServletRequest request, String defaultValue) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return defaultValue;
        }
        try {
            return value;
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 获取Date类型数据
     *
     * @param key
     * @param request
     * @param format  格式化格式
     * @return
     * @throws BusinessException
     */
    public Date getDate(String key, HttpServletRequest request, String format) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null) {
            throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式[" + format + "]不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Date类型数据	  默认返回null
     *
     * @param key
     * @param request
     * @param format  格式化格式
     * @return
     * @throws BusinessException
     */
    public Date optDate(String key, HttpServletRequest request, String format) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return null;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式[" + format + "]不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Date类型数据
     *
     * @param key
     * @param request
     * @param defaultValue 默认值
     * @param format       格式化格式
     * @return
     * @throws BusinessException
     */
    public Date optDate(String key, HttpServletRequest request, String format, String defaultValue) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
                return simpleDateFormat.parse(defaultValue);
            } catch (Exception e) {
                throw new BusinessException("参数" + defaultValue + "格式[" + format + "]不正确", CommonInfoUtil.PARAMERROR);
            }
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式[" + format + "]不正确", CommonInfoUtil.PARAMERROR);
        }
    }

    /**
     * 尝试获取Date类型数据
     *
     * @param key
     * @param request
     * @param defaultValue 默认值
     * @param format       格式化格式
     * @return
     * @throws BusinessException
     */
    public Date optDate(String key, HttpServletRequest request, String format, Date defaultValue) throws BusinessException {
        String value = request.getParameter(key);
        if (value == null || value.equals("")) {
            return defaultValue;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(value);
        } catch (Exception e) {
            throw new BusinessException("参数" + key + "格式[" + format + "]不正确", CommonInfoUtil.PARAMERROR);
        }
    }
}
